package com.example.jg;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.jg.adapter.HotAdatper;
import com.example.jg.base.BaseActivity;
import com.example.jg.bean.HotBean;
import com.example.jg.presenter.HotPresenter;
import com.example.jg.view.HotView;

import java.util.ArrayList;

import butterknife.BindView;

//adk   bd887b81c8a52ec6ae79fd83
public class MainActivity extends BaseActivity<HotPresenter> implements HotView {
    @BindView(R.id.recyler)
    RecyclerView Mrecycler;
    @BindView(R.id.bj)
    Button mbj;
    @BindView(R.id.qx)
    Button mqx;
    @BindView(R.id.sc)
    Button msc;
    private boolean a=true;
    private ArrayList<HotBean.RecentBean> recentBeans;
    private HotAdatper hotAdatper;
    private ArrayList<HotBean> hotBeans;
    private int pos;

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        mbj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mbj.setVisibility(View.INVISIBLE);
                mqx.setVisibility(View.VISIBLE);
                msc.setVisibility(View.VISIBLE);
                a=false;
               adapter();
            }
        });
        mqx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mbj.setVisibility(View.VISIBLE);
                mqx.setVisibility(View.INVISIBLE);
                msc.setVisibility(View.INVISIBLE);
                a=true;
                adapter();
            }
        });
        msc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mbj.setVisibility(View.VISIBLE);
                mqx.setVisibility(View.INVISIBLE);
                msc.setVisibility(View.INVISIBLE);
                a=true;
              hotAdatper.sc();
            }
        });
        hotAdatper.setOnContextClickListener(new HotAdatper.OnContextClickListener() {
            @Override
            public void OnContextClickListener(View v, int position) {
                pos=position;
                HotBean.RecentBean recentBean = recentBeans.get(position);
                String url = recentBean.getUrl();
                Intent intent = new Intent(MainActivity.this, WebActivity.class);
                intent.putExtra("url",url);
                startActivityForResult(intent,100);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==100&&resultCode==200){
            String sec = data.getStringExtra("sec");
            int i = Integer.parseInt(sec);
            recentBeans.get(pos).setSec(i);
            adapter();
        }
    }

    @Override
    protected void initView() {
        Mrecycler.setLayoutManager(new LinearLayoutManager(this));
        Mrecycler.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
        recentBeans = new ArrayList<>();
        hotBeans = new ArrayList<>();
        adapter();

    }
    public void adapter(){
        hotAdatper = new HotAdatper(this, recentBeans,a);
        Mrecycler.setAdapter(hotAdatper);
    }
    @Override
    protected void initPresenter() {
        mPresenter=new HotPresenter();
        mPresenter.getdata();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void onSuccess(HotBean data) {
        recentBeans.clear();
        recentBeans.addAll(data.getRecent());
        hotAdatper.notifyDataSetChanged();
    }

    @Override
    public void onFali(String error) {

    }

    @Override
    public void showToast(String str) {

    }
}
